<?php

/**
 * directory_types actions.
 *
 * @package    master_directory
 * @subpackage directory_types
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class directory_typesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
     $this->form = new DirectoryTypeForm();
  	
    $this->directory_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->directory_type = Doctrine_Core::getTable('DirectoryType')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->directory_type);
    
    $this->form = new DirectoryTypeForm($this->directory_type);
    
    return $this->renderPartial('directory_type', array('form'=>$this->form));
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new DirectoryTypeForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
	
    $form = new DirectoryTypeForm();

    $this->processForm($request, $form);

    //$this->setTemplate('new');
    
     $this->directory_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes();
     
    $this->form = new DirectoryTypeForm();
    
    return $this->renderPartial('directory_types_container',array('directory_types'=>$this->directory_types, 'form' => $this->form));
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($directory_type = Doctrine_Core::getTable('DirectoryType')->find(array($request->getParameter('id'))), sprintf('Object directory_type does not exist (%s).', $request->getParameter('id')));
    $this->form = new DirectoryTypeForm($directory_type);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($directory_type = Doctrine_Core::getTable('DirectoryType')->find(array($request->getParameter('id'))), sprintf('Object directory_type does not exist (%s).', $request->getParameter('id')));
    $form = new DirectoryTypeForm($directory_type);

    $this->processForm($request, $form);
    
    $this->directory_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes();
    
    $this->form = new DirectoryTypeForm();
    
    return $this->renderPartial('directory_types_container',array('directory_types'=>$this->directory_types, 'form' => $this->form));

    //$this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();

    $this->forward404Unless($directory_type = Doctrine_Core::getTable('DirectoryType')->find(array($request->getParameter('id'))), sprintf('Object directory_type does not exist (%s).', $request->getParameter('id')));
    //$directory_type->delete();
    
    $directory_type->setDeleted(1);
    $directory_type->save();
    
    $this->form = new DirectoryTypeForm();
    
    $this->directory_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes();
    
     return $this->renderPartial('directory_types_container',array('directory_types'=>$this->directory_types, 'form' => $this->form));

    //$this->redirect('directory_types/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $directory_type = $form->save();

      //$this->redirect('directory_types/edit?id='.$directory_type->getId());
    }
  }
}
